//---------------------------------------------------------------------------
#pragma hdrstop
#include "LoanShop.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
LoanShop::LoanShop(AnsiString id1, AnsiString name1)
{
	this->id1 = id1;
	this->name1 = name1;
	this->articles1 = new LoanArticle*[this->NUM_ARTICLES1];    //make a new pointer of array

	for (int i = 0; i < NUM_ARTICLES1; i++)
	{
		this->articles1[i] = NULL;
	}
	this->nrOfArticles1 = 0;
	this->nrOfPurchases1 = 0;
}
//---------------------------------------------------------------------------
LoanShop::~LoanShop()
{
	for (int i = 0; i <= nrOfArticles1; i++)
	{
		delete this->articles1[i];
	}
	delete [] articles1; // delete the array
}
//---------------------------------------------------------------------------
int LoanShop::getIndexOfArticle1(int id1)
{
	// loop for the array
	for (int index = 0; index < this->nrOfArticles1; index++)  // use nrOfArticles
	{   // check the name of user type and the articles
		if (id1 == this->articles1[index]->getArticleID1())
		{
			return index;
		}
	}
	return -1;
}
//---------------------------------------------------------------------------
int LoanShop::getIndexOfPurchase1(int id1)
{
	// loop for the array
	for (int index = 0; index < this->nrOfPurchases1; index++)  // use nrOfPurchases
	{   // check the name of user type and the articles
		if (id1 == this->purchaseIdList1[index])
		{
			return index;
		}
	}
	return -1;
}
//---------------------------------------------------------------------------
AnsiString LoanShop::getID1()		{ return this->id1; }
//---------------------------------------------------------------------------
AnsiString LoanShop::getName1()      { return this->name1; }
//---------------------------------------------------------------------------
int LoanShop::getCapacity1()			{ return this->NUM_ARTICLES1; }
//---------------------------------------------------------------------------
int LoanShop::getNrOfArticles1()     { return nrOfArticles1; }
//---------------------------------------------------------------------------
int LoanShop::getNrOfPurchases1()	{ return this->nrOfPurchases1; }
//---------------------------------------------------------------------------
LoanArticle* LoanShop::getArticleAtIndex1(int i1) { return this->articles1[i1]; }
//---------------------------------------------------------------------------
LoanArticle* LoanShop::getPurchaseAtIndex1(int i1)
{
	int id1 = this->purchaseIdList1[i1];	 // get the id from the purchase
	int index1 = getIndexOfArticle1(id1);   // get article index with the id
	return this->articles1[index1];		 // return the article
}
//---------------------------------------------------------------------------
int LoanShop::getPurchaseIdAtIndex1(int i1)
{
	return this->purchaseIdList1[i1];		// return the id
}
//---------------------------------------------------------------------------
int LoanShop::getPurchaseQtAtIndex1(int i1)
{
	return this->purchaseQtList1[i1];	   // return the quantity
}
//---------------------------------------------------------------------------
bool LoanShop::addArticle1(int id1, AnsiString nm1, int amount1, AnsiString kind1, double price1, double deposit1)
{
	if (this->nrOfArticles1 < this->NUM_ARTICLES1) // check if there is enough space
	{
		this->articles1[this->nrOfArticles1] = new LoanArticle(id1, nm1, amount1, kind1, price1, deposit1);
		this->nrOfArticles1++;
		return true;
	}
	else
	{
		return false;
	}
}
//---------------------------------------------------------------------------
bool LoanShop::soldSome1(int id1, int amount1)
{
	int artIndex1 = this->getIndexOfArticle1(id1); // get index
	if (artIndex1 != -1) // if it is exist
	{
		// if stock is bigger than what they want to remove
		if (this->articles1[artIndex1]->getNrOfItems1() >= amount1)
		{
        	// decrease the stock
			this->articles1[artIndex1]->decreaseNrOfItemsBy1(amount1);

			// then add this amount to the purchase
			int purIndex1 = this->getIndexOfPurchase1(id1);  // get index
			if (purIndex1 != -1) // if it is exist
			{
				// increase quantity
				this->purchaseQtList1[purIndex1] += amount1;
			}
			else
			{
				// add to purchase
				this->purchaseIdList1[this->nrOfPurchases1] = id1;
				this->purchaseQtList1[this->nrOfPurchases1] = amount1;
				this->nrOfPurchases1++;	// increase nrOfPurchases
			}
			return true;
		}
	}
	return false;
}
//---------------------------------------------------------------------------
bool LoanShop::unsoldSome1(int id1, int amount1)
{
	int artIndex1 = this->getIndexOfArticle1(id1); // get index
	if (artIndex1 != -1) // if it is exist
	{
		// remove amount from purchase
		int purIndex1 = this->getIndexOfPurchase1(id1);  // get index
		// check how much we want to remove
		if (amount1 < this->purchaseQtList1[purIndex1])
		{
			 // increase the stock
			 this->articles1[artIndex1]->increaseNrOfItemsBy1(amount1);

			 // remove some
			 this->purchaseQtList1[purIndex1] -= amount1;
		}
		else
		{
			// increase the stock
			this->articles1[artIndex1]->increaseNrOfItemsBy1(this->purchaseQtList1[purIndex1]);

			// remove all
			for (int i = purIndex1; i < this->nrOfPurchases1-1; i++)
			{
				this->purchaseIdList1[i] = this->purchaseIdList1[i+1];
				this->purchaseQtList1[i] = this->purchaseQtList1[i+1];
			}
			this->nrOfPurchases1--;  // decrease nrOfPurchases
		}
		return true;
	}
	return false;
}
//---------------------------------------------------------------------------
void LoanShop::unsoldAll1()
{
	// clear the whole purchase
	for (int purIndex = 0; purIndex < this->nrOfPurchases1; purIndex++)
	{
		// get the article index with the purchase id
		int artIndex = this->getIndexOfArticle1(this->purchaseIdList1[purIndex]);
		// increase the stock
		this->articles1[artIndex]->increaseNrOfItemsBy1(this->purchaseQtList1[purIndex]);
	}
	this->nrOfPurchases1 = 0;	// reset purchases quantity
}
//---------------------------------------------------------------------------
void LoanShop::clearAllPuchases1()		{ this->nrOfPurchases1 = 0; }
//---------------------------------------------------------------------------
double LoanShop::getTotalPrice1()
{
	double total1 = 0.0;
	for (int i = 0; i < this->nrOfPurchases1; i++)
	{
    	// get the price of the article with id of the purchase at index i and times it by the quantity
		total1 += articles1[getIndexOfArticle1(this->purchaseIdList1[i])]->getPrice1() * this->purchaseQtList1[i];
	}
	return total1;		// return the total price
}
//---------------------------------------------------------------------------
bool LoanShop::unsoldBroken1(int id1, int amount1)
{
	// remove the loan without adding it to the stock
	// remove amount from purchase
	int purIndex1 = this->getIndexOfPurchase1(id1);  // get index
	// check how much we want to remove
	if (amount1 < this->purchaseQtList1[purIndex1])
	{
		 // remove some
		 this->purchaseQtList1[purIndex1] -= amount1;
	}
	else
	{
		// remove all
		for (int i = purIndex1; i < this->nrOfPurchases1-1; i++)
		{
			this->purchaseIdList1[i] = this->purchaseIdList1[i+1];
			this->purchaseQtList1[i] = this->purchaseQtList1[i+1];
		}
		this->nrOfPurchases1--;  // decrease nrOfPurchases
	}
	return true;
}
//---------------------------------------------------------------------------
bool LoanShop::insertGuestLoanToPurchase1(int id1, int qt1)
{
	if (this->nrOfPurchases1 < this->NUM_ARTICLES1)
	{
		this->purchaseIdList1[this->nrOfPurchases1] = id1;
		this->purchaseQtList1[this->nrOfPurchases1] = qt1;
		this->nrOfPurchases1++;
		return true;
	}
	else
	{
        return false;
    }
}
//---------------------------------------------------------------------------
void LoanShop::sortAllArticlesByName1()
{
	for (int count = 0; count < this->nrOfArticles1; count++)
	{
		int smallest1 = count;
		for (int index = count+1; index < this->nrOfArticles1; index++)
		{
		if (this->articles1[smallest1]->getName1() > this->articles1[index]->getName1())
		   {
				smallest1 = index;
		   }
		}
		LoanArticle* temp;
		temp = articles1[count];
		this->articles1[count] = this->articles1[smallest1];
		this->articles1[smallest1] = temp;
	}
}
//---------------------------------------------------------------------------
void LoanShop::sortAllArticlesBySalesValue1()
{
	for (int count = 0; count < this->nrOfArticles1; count++)
	{
		int smallest1 = count;
		for (int index = count+1; index < this->nrOfArticles1; index++)
		{
			if ((this->articles1[smallest1]->getPrice1()*this->articles1[smallest1]->getNrOfItems1()) > (this->articles1[index]->getPrice1()*this->articles1[index]->getNrOfItems1()))
			{
				smallest1 = index;
			}
		}
		LoanArticle* temp;
		temp = this->articles1[count];
		this->articles1[count] = this->articles1[smallest1];
		this->articles1[smallest1] = temp;
	}
}
//---------------------------------------------------------------------------
void LoanShop::sortAllArticlesByKind1()
{
	sortAllArticlesByName1();
	for (int count = 0; count < this->nrOfArticles1; count++)
	{
		int smallest1 = count;
		for (int index = count+1; index < this->nrOfArticles1; index++)
		{
		if (this->articles1[smallest1]->getKind1() > this->articles1[index]->getKind1())
		   {
				smallest1= index;
		   }
		}
		LoanArticle* temp;
		temp = articles1[count];
		this->articles1[count] = this->articles1[smallest1];
		this->articles1[smallest1] = temp;
	}
}
//---------------------------------------------------------------------------
